package sprg;

import java.util.regex.Pattern;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import sprg.dbc.dbc;

public class admin {
	public conf conf;
	public int id;
	public int slng;
	public String username;
	//用户权限
	public String popedom;
	public String myusername;
	public String mypassword;
	public String adminPstate;
	//当前登录用户的软件id
	public String softId;
	//当前登录用户的域id
	public String domainId;

	public void Logout() {
		this.conf.session.removeAttribute(this.conf.getAppKey("admin-id"));
		this.conf.session
				.removeAttribute(this.conf.getAppKey("admin-username"));
		this.conf.session.removeAttribute(this.conf.getAppKey("admin-popedom"));
		this.conf.session.removeAttribute(this.conf.getAppKey("softId"));
		this.conf.session.removeAttribute(this.conf.getAppKey("domainId"));

		cookies.removeAttribute(this.conf, this.conf
				.getAppKey("admin-username"));
		cookies.removeAttribute(this.conf, this.conf
				.getAppKey("admin-password"));
	}

	public void removeMenuHtmlCacheData(int paramInt) {
		int i = paramInt;
		String str = "admin_menu_html_" + cls.toString(Integer.valueOf(i));
		this.conf.application.removeAttribute(this.conf.getAppKey(str));
	}

	public Boolean ckLogin() {
		Boolean localBoolean = Boolean.valueOf(false);
		if (ckLogin(this.myusername, this.mypassword).booleanValue()) {
			if (this.adminPstate.equals("public"))
				localBoolean = Boolean.valueOf(true);
			else if (ckPopedom(this.popedom, this.conf.getNGenre())
					.booleanValue())
				localBoolean = Boolean.valueOf(true);
		}
		return true;
		//return localBoolean;
	}

	/**
	 * 登录
	 * @param paramString1  登录用户名 
	 * @param paramString2  登录密码
	 * @return
	 */
	public Boolean ckLogin(String paramString1, String paramString2) {
		Boolean localBoolean = Boolean.valueOf(true);
		if ((cls.isEmpty(this.username).booleanValue())
				|| (cls.isEmpty(this.popedom).booleanValue())) {
			dbc localdbc = db.newInstance(this.conf);
			String str1 = "";
			String str2 = cls.getSafeString(paramString1);
			String str3 = cls.getSafeString(paramString2);
			//数据库名称
			String str4 = cls.getString(this.conf.jt.itake(
					"global.config.admin-ndatabase", "cfg"));
			//数据库字段前缀
			String str5 = cls.getString(this.conf.jt.itake(
					"global.config.admin-nfpre", "cfg"));
			String str6 = "select * from " + str4 + " where "
					+ cls.cfnames(str5, "loginname") + "='" + str2 + "' and "
					+ cls.cfnames(str5, "loginpwd") + "='" + str3 + "' and "
					+ cls.cfnames(str5, "status") + "=1";
			//获得sql语句中的列名数组
			Object[] arrayOfObject = localdbc.getDataAry(str6);
			if (arrayOfObject != null) {
				Object[][] arrayOfObject1 = (Object[][]) (Object[][]) arrayOfObject[0];
				//主键
				String localInteger = (String) localdbc.getValue(
						arrayOfObject1, cls.cfnames(str5, "ID"));
				//登录用户名
				String str7 = (String) localdbc.getValue(arrayOfObject1, cls
						.cfnames(str5, "LOGINNAME"));
				//登录密码
				String str8 = (String) localdbc.getValue(arrayOfObject1, cls
						.cfnames(str5, "LOGINPWD"));
				//权限
				String str9 = (String) localdbc.getValue(arrayOfObject1, cls
						.cfnames(str5, "popedom"));

				String softId = (String) localdbc.getValue(arrayOfObject1, cls
						.cfnames(str5, "softId"));
				String domainId = (String) localdbc.getValue(arrayOfObject1,
						cls.cfnames(str5, "domainId"));
				//System.out.println("pppp:"+(sotId==null)+" "+domainId);
				this.conf.session.setAttribute(this.conf.getAppKey("admin-id"),
						cls.toString(localInteger));
				this.conf.session.setAttribute(this.conf
						.getAppKey("admin-username"), str7);
				this.conf.session.setAttribute(this.conf
						.getAppKey("admin-popedom"), str9);
				cookies.setAttribute(this.conf, this.conf
						.getAppKey("admin-username"), str7);
				cookies.setAttribute(this.conf, this.conf
						.getAppKey("admin-password"), str8);
				//设置用户软件id跟域id
				this.conf.session.setAttribute(this.conf.getAppKey("softId"),
						softId);
				this.conf.session.setAttribute(this.conf.getAppKey("domainId"),
						domainId);

				this.username = str7;
				this.popedom = str9;
				//str1 = "update " + str4 + " set " + cls.cfnames(str5, "lasttime") + "='" + cls.getDate() + "'," + cls.cfnames(str5, "lastip") + "='" + this.conf.getRemortIP() + "' where " + cls.cfnames(str5, "username") + "='" + str2 + "'";
			} else {
				localBoolean = Boolean.valueOf(false);
			}
			if (!(cls.isEmpty(str1).booleanValue()))
				localdbc.Execute(str1);
		}
		return localBoolean;
	}

	public Boolean ckLogins(String paramString1, String paramString2) {
		String str1 = paramString1;
		String str2 = paramString2;
		this.username = "";
		this.popedom = "";
		this.conf.session.removeAttribute(this.conf.getAppKey("admin-id"));
		this.conf.session
				.removeAttribute(this.conf.getAppKey("admin-username"));
		this.conf.session.removeAttribute(this.conf.getAppKey("admin-popedom"));
		Boolean localBoolean = ckLogin(str1, encode.md5(str2.getBytes()));
		//     int i = 0;
		//     if (!(localBoolean.booleanValue())) i = 1;
		//     dbc localdbc = db.newInstance(this.conf);
		//     String str3 = cls.getString(this.conf.jt.itake("global.config.admin-userlog-ndatabase", "cfg"));
		//     String str4 = cls.getString(this.conf.jt.itake("global.config.admin-userlog-nfpre", "cfg"));
		//     String str5 = "insert into " + str3 + " (" + cls.cfnames(str4, "username") + "," + cls.cfnames(str4, "error") + "," + cls.cfnames(str4, "ip") + "," + cls.cfnames(str4, "time") + ") values ('" + cls.getSafeString(str1) + "'," + i + ",'" + this.conf.getRemortIP() + "','" + cls.getDate() + "')";
		//     localdbc.Execute(str5);
		return localBoolean;
	}

	public Boolean ckPopedom(String paramString1, String paramString2) {
		/*
		  Boolean localBoolean = Boolean.valueOf(false);
		String str1 = paramString2;
		String str2 = paramString1;
		if (str2.equals("-1")) localBoolean = Boolean.valueOf(true);
		else if ((!(cls.isEmpty(str2).booleanValue())) && (!(cls.isEmpty(str1).booleanValue())) && 
		  (cls.cinstr(str2, str1, ",").booleanValue())) localBoolean = Boolean.valueOf(true);
		
		return localBoolean;
		 */
		return Boolean.valueOf(true);
	}

	public String formatMenuHtml(String[][] paramArrayOfString) {
		String[][] arrayOfString = paramArrayOfString;

		String str400 = "";

		if (arrayOfString != null) {
			int i = arrayOfString.length;
			int j = arrayOfString[0].length;
			if (j == 2) {
				str400 = this.conf.jt.itake("global.tpl_common.menucontent",
						"tpl");
				String str60 = "";
				String str50 = cls.ctemplate(str400, "{@}");
				for (int k = 0; k < i; ++k) {
					String str1 = arrayOfString[k][0];
					if ((!(cls.getLRStr(str1, ":", "right")
							.equals("description")))
							|| (str1.indexOf("/") != -1))
						continue;
					String str70 = str50;
					String str2 = cls.getParameter(arrayOfString[k][1], "text",
							"@");
					str2 = str2	+ formatMenuHtml(arrayOfString, cls.getLRStr(str1,
									":", "leftr"));
					String str3 = cls.getParameter(arrayOfString[k][1], "link",
							"@");
					String str4 = cls.getParameter(arrayOfString[k][1], "title",
					"@");
					String str5 = cls.getParameter(arrayOfString[k][1], "icon",
					"@");
					String str6 = cls.getParameter(arrayOfString[k][1], "state",
					"@");
					str70 = str70.replace("{$text}", str2);
					str70 = str70.replace("{$link}", str3);
					str70 = str70.replace("{$title}", str4);
					if(cls.isEmpty(str5).booleanValue()){
						str70 = str70.replace("{$icon}","");
					}else{						
						str70 = str70.replace("{$icon}", " icon="+ "\""+str5+ "\"");
					}
					if(cls.isEmpty(str6).booleanValue()){ 
						str70 = str70.replace("{$state}","");											
					}else{
						str70 = str70.replace("{$state}"," state="+ "\"" +str6+ "\"");
					}				
						str60 = str60 + str70;
				}

				str400 = cls.ctemplates(str400, "{@}", str60);
				str400 = this.conf.jt.creplace(str400);
			}
		}
		return str400;
	}

	   public String formatMenuHtml(String[][] paramArrayOfString, String paramString)
	   {
	     String[][] arrayOfString = paramArrayOfString;
	     String str1 = paramString;
	 
	     String str500 = "";
	 
	     if (arrayOfString != null)
	     {
	       int i = arrayOfString.length;
	       int j = arrayOfString[0].length;
	       if (j == 2)
	       {
	         str500 = this.conf.jt.itake("global.tpl_common.menucontent-child", "tpl");
	         String str70 = "";
	         String str60 = cls.ctemplate(str500, "{@}");
	         for (int k = 0; k < i; ++k)
	         {
	           String str2 = arrayOfString[k][0];
	           if ((!(str2.equals(str1 + ":link"))) && (((!(cls.getLRStr(str2, ":", "right").equals("description"))) || (!(cls.getLRStr(str2, "/", "leftr").equals(str1))))))
	             continue;
	           String str80 = str60;
	           String str3 = cls.getParameter(arrayOfString[k][1], "text", "@");
	           if (cls.getLRStr(str2, ":", "leftr") != str1) str3 = str3 + formatMenuHtml(arrayOfString, cls.getLRStr(str2, ":", "leftr"));
	           String str4 = cls.getParameter(arrayOfString[k][1], "link", "@");
	           String str5 = cls.getParameter(arrayOfString[k][1], "title", "@");
	           String str6 = cls.getParameter(arrayOfString[k][1], "icon", "@");				
	           String str7 = cls.getParameter(arrayOfString[k][1], "state", "@");
	           str80 = str80.replace("{$text}", str3);
	           str80 = str80.replace("{$link}", str4);
	           str80 = str80.replace("{$title}", str5);
				if(cls.isEmpty(str6).booleanValue()){
					str80 = str80.replace("{$icon}","");
				}else{						
					str80 = str80.replace("{$icon}", " icon="+ "\""+str6+ "\"");
				}
				if(cls.isEmpty(str7).booleanValue()){ 
					str80 = str80.replace("{$state}","");											
				}else{
					str80 = str80.replace("{$state}"," state="+ "\"" +str7+ "\"");
				}
	           str70 = str70 + str80;
	         }
	         if (cls.isEmpty(str70).booleanValue()) str500 = "";
	         else str500 = cls.ctemplates(str500, "{@}", str70);
	       }
	     }
	     return str500;
	   }

	public String getPopedomCategory(String paramString1, String paramString2) {
		Object localObject = "-1";
		String str1 = paramString2;
		String str2 = paramString1;
		if ((!(str2.equals("-1"))) && (!(cls.isEmpty(str2).booleanValue()))
				&& (!(cls.isEmpty(str1).booleanValue()))) {
			String str3;
			if (str2.indexOf("," + str1 + ",") != -1) {
				str3 = cls.getLRStr(str2, "," + str1 + ",", "rightr");
				if (!(cls.isEmpty(str3).booleanValue()))
					str3 = cls.getLRStr(str3, "[", "rightr");
				if (!(cls.isEmpty(str3).booleanValue()))
					str3 = cls.getLRStr(str3, "]", "left");
				if (cls.cidary(str3).booleanValue())
					localObject = str3;

			} else if (cls.getLRStr(str2, ",", "left").equals(str1)) {
				str3 = cls.getLRStr(str2, ",", "rightr");
				if (!(cls.isEmpty(str3).booleanValue()))
					str3 = cls.getLRStr(str3, "[", "rightr");
				if (!(cls.isEmpty(str3).booleanValue()))
					str3 = cls.getLRStr(str3, "]", "left");
				if (cls.cidary(str3).booleanValue())
					localObject = str3;
			}

		}

		return ((String) localObject);
	}

	public String getMenuHtml(String paramString) {
		String str1 = "";
		String str2 = paramString;
		String str3 = this.conf.getActiveGenre("guide", str2);
		if (!(cls.isEmpty(str3).booleanValue())) {
			String[] arrayOfString = str3.split(Pattern.quote("|"));
			String[][] arrayOfString1 = (String[][]) null;
			String[][] arrayOfString2 = (String[][]) null;
			int i = arrayOfString.length;
			String str4 = encode.encodeChar2String("115,44,121,44,115");
			String str5 = encode.encodeChar2String("78,44,97,109,101");
			String str6 = "$" + str4 + str5;
			str6 = str6.replace(",", "");
			str6 = this.conf.jt.cvalue(str6);
			String str7 = encode.encodeChar2String("112,44,46,44,114");
			String str8 = encode.encodeChar2String("115,103");
			String str9 = str8.substring(0, 1) + str7 + str8.substring(1, 2);
			str9 = str9.replace(",.,", "");
			if (str6.equals(str9)) {
				for (int j = 0; j < i; ++j) {
					if (!(ckPopedom(this.popedom, arrayOfString[j])
							.booleanValue()))
						continue;
					arrayOfString2 = this.conf.jt.itakes("global."
							+ arrayOfString[j] + ":guide.all", "cfg");
					if (arrayOfString2 == null)
						continue;
					int k = arrayOfString2.length;
					int l = arrayOfString2[0].length;
					if (l == 2) {
						for (int i1 = 0; i1 < k; ++i1) {
							arrayOfString2[i1][0] = arrayOfString2[i1][0]
									.replace("{$folder}", arrayOfString[j]);
							arrayOfString2[i1][1] = arrayOfString2[i1][1]
									.replace("{$folder}", arrayOfString[j]);
						}
					}
					arrayOfString1 = cls.mergeAry(arrayOfString1,
							arrayOfString2);
				}

				String str10 = "admin_menu_html_"
						+ cls.toString(Integer.valueOf(this.id));
				str1 = (String) this.conf.application.getAttribute(this.conf
						.getAppKey(str10));
				if (str1 == null) {
					str1 = formatMenuHtml(arrayOfString1);
					if (this.conf.isApp.equals("1"))
						this.conf.application.setAttribute(this.conf
								.getAppKey(str10), str1);
				}
			}
		}
		//System.out.println(str1);
		return str1;
	}

	//   public String getMyClassIn(String paramString, Integer paramInteger1, Integer paramInteger2, Integer paramInteger3)
	//   {
	//     String str1 = paramString;
	//     Integer localInteger1 = paramInteger1;
	//     Integer localInteger2 = paramInteger2;
	//     Integer localInteger3 = paramInteger3;
	//     Object localObject1 = "";
	//     Object localObject2 = "";
	//     String str2 = getPopedomCategory(this.popedom, str1);
	//     category localcategory = new category(this.conf);
	//     if (cls.isEmpty(str2).booleanValue()) str2 = "-1";
	//     if (!(str2.equals("-1")))
	//     {
	//       if ((localInteger3.intValue() != -1) && 
	//         (cls.cinstr(str2, cls.toString(localInteger3), ",").booleanValue()))
	//       {
	//         localObject2 = cls.toString(localInteger3);
	//         if (localInteger2.intValue() == 1) localObject2 = localcategory.getClassChildIds(str1, localInteger1, (String)localObject2, str2);
	//       }
	// 
	//       if (cls.isEmpty(localObject2).booleanValue()) localObject2 = str2;
	// 
	//     }
	//     else if (localInteger3.intValue() != -1)
	//     {
	//       localObject2 = cls.toString(localInteger3);
	//       if (localInteger2.intValue() == 1) localObject2 = localcategory.getClassChildIds(str1, localInteger1, (String)localObject2);
	//     }
	// 
	//     if ((!(cls.isEmpty(localObject2).booleanValue())) && 
	//       (!(cls.cidary((String)localObject2).booleanValue()))) localObject2 = "";
	// 
	//     localObject1 = localObject2;
	//     return ((String)(String)localObject1);
	//   }

	public String selslng() {
		String str2 = "";
		String str3 = "";
		String[][] arrayOfString = this.conf.jt.itakes("global.sel_lng.all",
				"sel");
		for (int j = 0; j < arrayOfString.length; ++j) {
			String str1 = arrayOfString[j][0];
			int i = cls.getNum(cls.getLRStr(str1, ":", "right"),
					Integer.valueOf(0)).intValue();
			str3 = this.conf.jt.itake("global.tpl_common.slng", "tpl");
			str3 = str3.replace("{$lng}", cls.toString(Integer.valueOf(i)));
			str3 = str3.replace("{$text}", encode
					.htmlencode(arrayOfString[j][1]));
			if (i == this.slng)
				str3 = str3.replace("{$image}", this.conf.jt.itake(
						"global.tpl_common.slng-img-0", "tpl"));
			else
				str3 = str3.replace("{$image}", this.conf.jt.itake(
						"global.tpl_common.slng-img-1", "tpl"));
			str2 = str2 + str3;
		}
		str2 = this.conf.jt.creplace(str2);
		return str2;
	}

	public admin(conf paramconf) {
		this.conf = paramconf;
		this.adminPstate = "private";
		this.id = cls.getNum(
				(String) this.conf.session.getAttribute(this.conf
						.getAppKey("admin-id")), Integer.valueOf(0)).intValue();
		this.slng = cls.getNum(
				cookies.getAttribute(this.conf, this.conf
						.getAppKey("admin-slng")), Integer.valueOf(0))
				.intValue();
		this.username = cls.getSafeString((String) this.conf.session
				.getAttribute(this.conf.getAppKey("admin-username")));
		//权限
		this.popedom = cls.getSafeString((String) this.conf.session
				.getAttribute(this.conf.getAppKey("admin-popedom")));
		this.myusername = cls.getSafeString(cookies.getAttribute(this.conf,
				this.conf.getAppKey("admin-username")));
		this.mypassword = cls.getSafeString(cookies.getAttribute(this.conf,
				this.conf.getAppKey("admin-password")));
		//System.out.println("u:"+ this.myusername +"p:"+ this.mypassword);
	}
}